package znbkCNtxl.CNTXL_PPT;

import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.annotation.SuppressLint;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.view.animation.Animation;
import android.webkit.JavascriptInterface;
import android.webkit.ValueCallback;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.znbk.znbklibrary.base.AnswerViewPagerAdapter;
import com.example.znbk.znbklibrary.base.BaseFragment;
import com.example.znbk.znbklibrary.dialog.MaskDialog;
import com.example.znbk.znbklibrary.widget.PleaseReadyProgress;
import com.example.znbk.znbklibrary.widget.ShadowDrawable;
import com.lancoo.znbkxx.R;
import com.tt.CoreType;
import com.tt.QType;
import com.tt.SkEgnManager;
import com.tt.listener.OnRecordListener;
import com.tt.widget.AudioRecoderUtils;
import com.tt.widget.JSONTool;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import znbkCNtxl.CNTXL_YDJD.CNtxlYdjdAnswerAdapter;
import znbkCNtxl.CNtxlActivity;
import znbkCNtxl.GetNtxlJson.CNtxlPaperEntity;

@SuppressLint("ValidFragment")
public class CNtxlPptFragment extends BaseFragment {

    private int fromModel = -1;
    private List<CNtxlPaperEntity> CNtxlPaperEntity;
    private int quesIndex = -1;
    private CallBackFragmentPpt callBackFragmentPpt;
    public CNtxlPptFragment(int fromModel,List<CNtxlPaperEntity> CNtxlPaperEntity, int quesIndex, CallBackFragmentPpt callBackFragmentPpt){
        this.fromModel = fromModel;
        this.CNtxlPaperEntity = CNtxlPaperEntity;
        this.quesIndex = quesIndex;
        this.callBackFragmentPpt = callBackFragmentPpt;
    }

    @Override
    protected int createLayout() {
        return R.layout.cntxl_fragment_ppt;
    }

    String FunctionXY = "<html>\n" +
            "    <head>\n" +
            "    <meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">\n" +
            "        <script type=\"text/javascript\">\n" +
            "\n" +
            "             function scrollTop(id){\n" +
            "                var input = document.getElementById(id); // 根据ID取得元素对象\n" +
            "                var sizeObj = input.getBoundingClientRect(); // 取得元素距离窗口的绝对位置\n" +
            "                var inputOffsetTop = sizeObj.top + bodyOffset.top; // 距顶部\n" +
            "                return inputOffsetTop;\n" +
            "             }\n" +
            "\n" +
            "             function slideTo (targetPageY) {\n" +
            "              var timer = setInterval(function () {\n" +
            "                  var currentY = document.documentElement.scrollTop || document.body.scrollTop;//当前及滑动中任意时刻位置\n" +
            "                  var distance = targetPageY > currentY ? targetPageY - currentY : currentY - targetPageY;//剩余距离\n" +
            "                  var speed = Math.ceil(distance/10);//每时刻速度\n" +
            "                  if (currentY == targetPageY) {\n" +
            "                   clearInterval(timer);\n" +
            "                  } else {\n" +
            "                   window.scrollTo(0,targetPageY > currentY ? currentY + speed : currentY - speed);\n" +
            "                  }\n" +
            "                 },10);\n" +
            "             }\n" +
            "        </script>\n" +
            "    </head>\n" +
            "</html>";
    String Function = "<html>\n" +
            "<head>\n" +
            "    <meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">\n" +
            "    <script type=\"text/javascript\">\n" +
            "\t  function CalledByJavaCodeWithPara(id,arg) {\n" +
            "\t       document.getElementById(id,).innerHTML = arg;\n" +
            "      }\n" +
            "    </script>\n" +
            "</head>\n" +
            "</html>";
    String Function_backgroundColor = "<html>\n" +
            "<head>\n" +
            "    <meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">\n" +
            "    <script type=\"text/javascript\">\n" +
            "\t  function setBackGroundColor(id,bgColor) {\n" +
            "\t       document.getElementById(id,).style.backgroundColor = bgColor;\n" +
            "      }\n" +
            "    </script>\n" +
            "</head>\n" +
            "</html>";

    private WebView mWvQuesBody;
    private RelativeLayout mRlMyRecordBg;
    private ImageView mIvMyRecordMi;
    private Drawable drawableMi;
    private TextView mTvHintAnswerType;

    private ViewPager mVpAnswer;
    public AnswerViewPagerAdapter mAnswerViewPagerAdapter;
    private ArrayList<View> list;

    @SuppressLint("NewApi")
    @Override
    protected void initView(View view) {

        clickWhichIndex = CNtxlPaperEntity.get(quesIndex).getQuesFromTK().
                getChildList().get(0).getSubChildList().get(0).getSubChildItem().get(0).getSortIndex();

        mWvQuesBody = view.findViewById(R.id.wv_quesBody);
        mWvQuesBody.setBackgroundColor(0); // 设置背景色
        mWvQuesBody.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
        // 启用javascript
        mWvQuesBody.getSettings().setJavaScriptEnabled(true);
        mWvQuesBody.setWebViewClient(new WebViewClient());
        mWvQuesBody.setWebChromeClient(new WebChromeClient());

        String QuesBody = CNtxlPaperEntity.get(quesIndex).getQuesFromTK().getChildList().get(0).getSubChildList().get(0).getSubChildAsk();
        QuesBody = QuesBody.replaceAll("clickFunction","Android.clickFunction");
        mWvQuesBody.loadData(FunctionXY+Function+Function_backgroundColor+QuesBody,"text/html","utf-8");
        mWvQuesBody.addJavascriptInterface(new JSInterface(), "Android");

        int SubmitState = CNtxlPaperEntity.get(quesIndex).getFavouriteAndNote().getSubmitState();
        if(SubmitState!=2){
            int Size = CNtxlPaperEntity.get(quesIndex).getQuesFromTK().getChildList().
                    get(0).getSubChildList().get(0).getSubChildItem().size();
            for(int i=0;i<Size;i++){
                CNtxlPaperEntity.get(quesIndex).getFavouriteAndNote().getChildList().
                        get(0).getSubChildList().get(0).getSubChildItem().get(i).setStuAnswer("");
            }
        }
        //显示Answer信息
        mVpAnswer = view.findViewById(R.id.vp_answerPpt);
        list = new ArrayList<>();
        list.clear();
        RecyclerView mRvAnswer = new RecyclerView(getActivity());
        mRvAnswer.setLayoutManager(new LinearLayoutManager(getActivity()));//必须先设置LayoutManager
        CNtxlPptAnswerAdapter mAnswerAdapter = new CNtxlPptAnswerAdapter(getActivity(),
                CNtxlPaperEntity,quesIndex,new CNtxlPptAnswerAdapter.CallBackAdapterPpt() {
            @Override
            public void callHideRecordMiView() {
                hideRecordMiView();
            }

            @Override
            public void callClickWhichIndex(String stuAnswer) {
                //选择答案后刷新界面显示
                Log.e("mTvOptionContentA", "===========callClickWhichIndex===================" + clickWhichIndex);
                int SortIndexFirst = CNtxlPaperEntity.get(quesIndex).getQuesFromTK().getChildList().
                        get(0).getSubChildList().get(0).getSubChildItem().get(0).getSortIndex();
                Log.e("mTvOptionContent", "===========SortIndex===================" + SortIndexFirst);
                CNtxlPaperEntity.get(quesIndex).getFavouriteAndNote().getChildList().
                        get(0).getSubChildList().get(0).getSubChildItem().get(clickWhichIndex - SortIndexFirst).setStuAnswer(stuAnswer);

                int SubmitState = CNtxlPaperEntity.get(quesIndex).getFavouriteAndNote().getSubmitState();
                int SubChildItemSize = CNtxlPaperEntity.get(quesIndex).getFavouriteAndNote().getChildList().get(0).
                        getSubChildList().get(0).getSubChildItem().size();
                for(int j=0;j<SubChildItemSize;j++) {

                    String StuAnswer = CNtxlPaperEntity.get(quesIndex).getFavouriteAndNote().getChildList().
                            get(0).getSubChildList().get(0).getSubChildItem().get(j).getStuAnswer();
                    String ItemAnswer = CNtxlPaperEntity.get(quesIndex).getFavouriteAndNote().getChildList().
                            get(0).getSubChildList().get(0).getSubChildItem().get(j).getItemAnswer();
                    int SortIndex = CNtxlPaperEntity.get(quesIndex).getQuesFromTK().getChildList().
                            get(0).getSubChildList().get(0).getSubChildItem().get(j).getSortIndex();

                    if(clickWhichIndex==SortIndex) {

                        if(SubmitState==2){

                        }else {
                            mWvQuesBody.loadUrl("javascript:setBackGroundColor('" + "blank" + clickWhichIndex +
                                    "','" + "#326DE9" + "')");
                            if (StuAnswer.equals("")) {
                                mWvQuesBody.loadUrl("javascript:CalledByJavaCodeWithPara('" + "blank" + (clickWhichIndex) +
                                        "','" + "<font color=\"#ffffff\">" + (clickWhichIndex) + "</font>')");
                            } else {
                                mWvQuesBody.loadUrl("javascript:CalledByJavaCodeWithPara('" + "blank" + (clickWhichIndex) +
                                        "','" + "<font color=\"#ffffff\">" + (clickWhichIndex) + "." + StuAnswer + "</font>')");
                            }
                        }
                    }else{

                        if(SubmitState==2){

                        }else {
                            mWvQuesBody.loadUrl("javascript:setBackGroundColor('" + "blank" + SortIndex +
                                    "','" + "#00000000" + "')");
                            if (StuAnswer.equals("")) {
                                mWvQuesBody.loadUrl("javascript:CalledByJavaCodeWithPara('" + "blank" + SortIndex +
                                        "','" + "<font color=\"#326DE9\">" + SortIndex + "</font>')");
                            } else {
                                mWvQuesBody.loadUrl("javascript:CalledByJavaCodeWithPara('" + "blank" + SortIndex +
                                        "','" + "<font color=\"#326DE9\">" + SortIndex + "." + StuAnswer + "</font>')");
                            }
                        }
                    }
                }

            }

            @Override
            public void callRefreshClickAnswer() {
                refreshRecordAnswer();
            }

        });
        mAnswerAdapter.setHasStableIds(true);
        mRvAnswer.setAdapter(mAnswerAdapter);
        int ItemSize  = CNtxlPaperEntity.get(quesIndex).getQuesFromTK().getTotalItemCount();
        mRvAnswer.setItemViewCacheSize(ItemSize);
        list.add(mRvAnswer);
        mAnswerViewPagerAdapter = new AnswerViewPagerAdapter(list);
        mVpAnswer.setAdapter(mAnswerViewPagerAdapter);
//        bkfxPaperEntity.getPaperAnswer().get(quesIndex).setCurrentSubChildAnswerIndex(0);
        mVpAnswer.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i1) {

            }

            @Override
            public void onPageSelected(int i) {
//                bkfxPaperEntity.getPaperAnswer().get(quesIndex).setCurrentSubChildAnswerIndex(i);

            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });

        //显示话咪
        mRlMyRecordBg = view.findViewById(R.id.rl_myRecordBg);
        mRlMyRecordBg.setVisibility(View.GONE);
        mRlMyRecordBg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                return;
            }
        });
        mIvMyRecordMi = view.findViewById(R.id.iv_myRecordMi);
        drawableMi = mIvMyRecordMi.getDrawable();

        mTvHintAnswerType = view.findViewById(R.id.tv_hintAnswerType);
        ObjectAnimator objectAnimator = ObjectAnimator.ofInt(mTvHintAnswerType,"textColor",0xffffffff,0xffff00aa);
        objectAnimator.setDuration(2000);
        objectAnimator.setRepeatCount(Animation.INFINITE);
        objectAnimator.setRepeatMode(ValueAnimator.REVERSE);
        objectAnimator.start();

        /****************************************************************************************/
        final Handler mH1 = new Handler();
        mH1.postDelayed(new Runnable() {
            @SuppressLint("NewApi")
            @Override
            public void run() {

                int SortIndex = CNtxlPaperEntity.get(quesIndex).getQuesFromTK().
                        getChildList().get(0).getSubChildList().get(0).getSubChildItem().get(0).getSortIndex();
                Log.e("callClickWhichIndexAAA", "====11111=======onReceiveValue value=============" + SortIndex);

                mWvQuesBody.evaluateJavascript("javascript:scrollTop('"+"blank"+SortIndex+"')", new ValueCallback<String>() {

                    @Override
                    public void onReceiveValue(String value) {
                        int SubmitState = CNtxlPaperEntity.get(quesIndex).getFavouriteAndNote().getSubmitState();

                        int SubChildItemSize = CNtxlPaperEntity.get(quesIndex).getQuesFromTK().getChildList().get(0).
                                getSubChildList().get(0).getSubChildItem().size();
                        for(int i=0;i<SubChildItemSize;i++){
                            String StuAnswer = CNtxlPaperEntity.get(quesIndex).getFavouriteAndNote().getChildList().
                                    get(0).getSubChildList().get(0).getSubChildItem().get(i).getStuAnswer();
                            String ItemAnswer = CNtxlPaperEntity.get(quesIndex).getFavouriteAndNote().getChildList().
                                    get(0).getSubChildList().get(0).getSubChildItem().get(i).getItemAnswer();
                            int SortIndex = CNtxlPaperEntity.get(quesIndex).getQuesFromTK().getChildList().get(0).
                                    getSubChildList().get(0).getSubChildItem().get(i).getSortIndex();

                            if(SubmitState==2){

                                if (ItemAnswer.equals(StuAnswer)) {
                                    mWvQuesBody.loadUrl("javascript:CalledByJavaCodeWithPara('" + "blank" + SortIndex + "','" +
                                            "<font color=\"#00b30d\">" + SortIndex +"." + StuAnswer + "</font>')");
                                } else {
                                    mWvQuesBody.loadUrl("javascript:CalledByJavaCodeWithPara('" + "blank" + SortIndex + "','" +
                                            "<font color=\"#ff0000\">" + SortIndex + "." + StuAnswer + "</font>"+
                                            "<font color=\"#00b30d\">("+ItemAnswer+")"+"</font>"+"')");
                                }
                            }else {
                                if(clickWhichIndex==SortIndex){
                                    mWvQuesBody.loadUrl("javascript:setBackGroundColor('" + "blank" + SortIndex +
                                            "','" + "#326DE9" + "')");
                                    if (StuAnswer.equals("")) {
                                        mWvQuesBody.loadUrl("javascript:CalledByJavaCodeWithPara('" + "blank" + SortIndex +
                                                "','" + "<font color=\"#ffffff\">" + SortIndex + "</font>')");
                                    } else {
                                        mWvQuesBody.loadUrl("javascript:CalledByJavaCodeWithPara('" + "blank" + SortIndex +
                                                "','" + "<font color=\"#ffffff\">" + SortIndex + "." + StuAnswer + "</font>')");
                                    }
                                }else{
                                    mWvQuesBody.loadUrl("javascript:setBackGroundColor('" + "blank" + SortIndex +
                                            "','" + "#00000000" + "')");
                                    if (StuAnswer.equals("")) {
                                        mWvQuesBody.loadUrl("javascript:CalledByJavaCodeWithPara('" + "blank" + SortIndex +
                                                "','" + "<font color=\"#326DE9\">" + SortIndex + "</font>')");
                                    } else {
                                        mWvQuesBody.loadUrl("javascript:CalledByJavaCodeWithPara('" + "blank" + SortIndex +
                                                "','" + "<font color=\"#326DE9\">" + SortIndex + "." + StuAnswer + "</font>')");
                                    }
                                }

                            }
                        }
                    }
                });
                mH1.removeCallbacks(this);
            }
        },500);
        /****************************************************************************************/
    }

    private int clickWhichIndex = -1;
    @SuppressWarnings("unused")
    private final class JSInterface{

        @JavascriptInterface
        public void clickFunction(final int position) {
            //在webview中进行点击选择需要作答的区间
            getActivity().runOnUiThread(new Runnable() {
                @SuppressLint("NewApi")
                @Override
                public void run() {
                    Log.e("LOGTAG", "===========JSInterface============" + String.valueOf(position));
                    clickWhichIndex = position;

                    mWvQuesBody.evaluateJavascript("javascript:scrollTop('"+"blank"+clickWhichIndex+"')", new ValueCallback<String>() {
                        @Override
                        public void onReceiveValue(String value) {
                            writeStuAnswer();
                        }
                    });

                }
            });
        }

    }



    private void showRecordMiView(){
        if(mRlMyRecordBg.getVisibility()==View.GONE) {
            ObjectAnimator translationY = ObjectAnimator.ofFloat(mRlMyRecordBg, View.TRANSLATION_Y, 300f, 0f);
            AnimatorSet setShow = new AnimatorSet();
            setShow.playTogether(translationY);
            setShow.setDuration(300);
            setShow.addListener(new Animator.AnimatorListener() {
                @Override
                public void onAnimationStart(Animator animation) {
                    mRlMyRecordBg.setVisibility(View.VISIBLE);
                }

                @Override
                public void onAnimationEnd(Animator animation) {

                }

                @Override
                public void onAnimationCancel(Animator animation) {

                }

                @Override
                public void onAnimationRepeat(Animator animation) {

                }
            });
            setShow.start();
        }
    }
    private void hideRecordMiView(){
        if(mRlMyRecordBg.getVisibility()==View.VISIBLE) {
            ObjectAnimator translationY = ObjectAnimator.ofFloat(mRlMyRecordBg, View.TRANSLATION_Y, 0f, 300f);
            AnimatorSet setShow = new AnimatorSet();
            setShow.playTogether(translationY);
            setShow.setDuration(300);
            setShow.addListener(new Animator.AnimatorListener() {
                @Override
                public void onAnimationStart(Animator animation) {

                }

                @Override
                public void onAnimationEnd(Animator animation) {
                    mRlMyRecordBg.setVisibility(View.GONE);

                    //执行提交操作
                    final Handler mH = new Handler();
                    mH.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            callBackFragmentPpt.submitKgtQuesAnswer();
                            CNtxlActivity.mVpCNtxl.setScrollble(true);
                            mH.removeCallbacks(this);
                        }
                    },1000);
                }

                @Override
                public void onAnimationCancel(Animator animation) {

                }

                @Override
                public void onAnimationRepeat(Animator animation) {

                }
            });
            setShow.start();
        }
    }

    public void setUserVisibleHint(boolean isVisibleToUser) {

        if(isVisibleToUser) {
            final Handler mH1 = new Handler();
            mH1.postDelayed(new Runnable() {
                @SuppressLint("NewApi")
                @Override
                public void run() {
                    if(mVpAnswer!=null) {
                        writeStuAnswer();
                        mH1.removeCallbacks(this);
                    }else{
                        mH1.post(this);
                    }
                }
            },1000);
        }

        if(isVisibleToUser){
            Handler mH = new Handler();
            mH.postDelayed(new Runnable() {
                @Override
                public void run() {
                    int SubmitState = CNtxlPaperEntity.get(quesIndex).getFavouriteAndNote().getSubmitState();
                    if (SubmitState != 2) {
                        showRecordMiView();
                        mRecoderUtils = new AudioRecoderUtils();
                        SkEgnManager.getInstance(getActivity()).startRecord(mCoreType, "", QType.QTYPE_EMPTY, mOnRecordListener);
                        mRecoderUtils.startRecord();
                    }else{
                        mRlMyRecordBg.setVisibility(View.GONE);
                        SkEgnManager.getInstance(getActivity()).stopRecord();
                        if(mRecoderUtils!=null) {
                            mRecoderUtils.stopRecord();
                        }
                    }
                }
            },300);

        }else{
            if(mRlMyRecordBg!=null) {
                mRlMyRecordBg.setVisibility(View.GONE);
                SkEgnManager.getInstance(getActivity()).stopRecord();
                if(mRecoderUtils!=null) {
                    mRecoderUtils.stopRecord();
                }
            }
        }
    }
    /****************************************************************************/
    private AudioRecoderUtils mRecoderUtils;
    private String  mCoreType = CoreType.EN_ASR_REC;
    /*
     * 话咪录音操作
     * */
    OnRecordListener mOnRecordListener = new OnRecordListener() {
        @Override
        public void onRecordStart() {
            //开始录制
            Log.e("==onRecordingAA==","========vad_status==========="+"121212121212");
        }
        @Override
        public void onRecording(int vad_status, int sound_intensity) {
            //录制中
            int SubmitState = CNtxlPaperEntity.get(quesIndex).getFavouriteAndNote().getSubmitState();
            if(SubmitState==2){
                SkEgnManager.getInstance(getActivity()).stopRecord();
                mRecoderUtils.stopRecord();
            }
            Log.e("==onRecordingAA==","========vad_status==========="+vad_status);
            Log.e("==onRecordingBB==","=========sound_intensity==========="+sound_intensity);
            showRecordVolumeSize(sound_intensity);
            if(vad_status==2){
                SkEgnManager.getInstance(getActivity()).stopRecord();
                mRecoderUtils.stopRecord();
            }
        }
        @Override
        public void onRecordEnd(String result) {
            //录制结束
            try {
                final JSONObject json = new JSONObject(result);
                Log.e("17kouyuAAA", "返回json===>" + result);
                String choiceResult = "";
                if(!getResult(result).equals("")) {
                    choiceResult = getResult(result);
                }
                Log.e("==AFGAFG==","========KKKKK》》==========="+choiceResult);
                int OptionSize = CNtxlPaperEntity.get(quesIndex).getQuesFromTK().getChildList().//获取小题对应的选项个数
                        get(0).getSubChildList().get(0).getOptionList().size();
                int SortIndexFirst = CNtxlPaperEntity.get(quesIndex).getQuesFromTK().getChildList().get(0).
                        getSubChildList().get(0).getSubChildItem().get(0).getSortIndex();

                if(choiceResult.equals("a")||choiceResult.equals("hey")||
                        choiceResult.equals("b")||  choiceResult.equals("boy")||
                        choiceResult.equals("c")||
                        choiceResult.equals("d")||choiceResult.equals("dog")||
                        choiceResult.equals("e")||
                        choiceResult.equals("f")||
                        choiceResult.equals("g")||
                        choiceResult.equals("h")||
                        choiceResult.equals("i")||
                        choiceResult.equals("j")||
                        choiceResult.equals("k")){

                    if(choiceResult.equals("a")||choiceResult.equals("hey")){
                        if(OptionSize>=1) {
                            CNtxlPaperEntity.get(quesIndex).getFavouriteAndNote().getChildList().
                                    get(0).getSubChildList().get(0).getSubChildItem().get(clickWhichIndex-SortIndexFirst).setStuAnswer("A");
                        }
                    }else if(choiceResult.equals("b")||  choiceResult.equals("boy")){
                        if(OptionSize>=2) {
                            CNtxlPaperEntity.get(quesIndex).getFavouriteAndNote().getChildList().
                                    get(0).getSubChildList().get(0).getSubChildItem().get(clickWhichIndex-SortIndexFirst).setStuAnswer("B");
                        }
                    }else if(choiceResult.equals("c")){
                        if(OptionSize>=3) {
                            CNtxlPaperEntity.get(quesIndex).getFavouriteAndNote().getChildList().
                                    get(0).getSubChildList().get(0).getSubChildItem().get(clickWhichIndex-SortIndexFirst).setStuAnswer("C");
                        }
                    }else if( choiceResult.equals("d")||choiceResult.equals("dog")){
                        if(OptionSize>=4) {
                            CNtxlPaperEntity.get(quesIndex).getFavouriteAndNote().getChildList().
                                    get(0).getSubChildList().get(0).getSubChildItem().get(clickWhichIndex-SortIndexFirst).setStuAnswer("D");
                        }
                    }else if( choiceResult.equals("e")){
                        if(OptionSize>=5) {
                            CNtxlPaperEntity.get(quesIndex).getFavouriteAndNote().getChildList().
                                    get(0).getSubChildList().get(0).getSubChildItem().get(clickWhichIndex-SortIndexFirst).setStuAnswer("E");
                        }
                    }else if( choiceResult.equals("f")){
                        if(OptionSize>=6) {
                            CNtxlPaperEntity.get(quesIndex).getFavouriteAndNote().getChildList().
                                    get(0).getSubChildList().get(0).getSubChildItem().get(clickWhichIndex-SortIndexFirst).setStuAnswer("F");
                        }
                    }else if( choiceResult.equals("g")){
                        if(OptionSize>=7) {
                            CNtxlPaperEntity.get(quesIndex).getFavouriteAndNote().getChildList().
                                    get(0).getSubChildList().get(0).getSubChildItem().get(clickWhichIndex-SortIndexFirst).setStuAnswer("G");
                        }
                    }else if( choiceResult.equals("h")){
                        if(OptionSize>=8) {
                            CNtxlPaperEntity.get(quesIndex).getFavouriteAndNote().getChildList().
                                    get(0).getSubChildList().get(0).getSubChildItem().get(clickWhichIndex-SortIndexFirst).setStuAnswer("H");
                        }
                    }else if( choiceResult.equals("i")){
                        if(OptionSize>=9) {
                            CNtxlPaperEntity.get(quesIndex).getFavouriteAndNote().getChildList().
                                    get(0).getSubChildList().get(0).getSubChildItem().get(clickWhichIndex-SortIndexFirst).setStuAnswer("I");
                        }
                    }else if( choiceResult.equals("j")){
                        if(OptionSize>=9) {
                            CNtxlPaperEntity.get(quesIndex).getFavouriteAndNote().getChildList().
                                    get(0).getSubChildList().get(0).getSubChildItem().get(clickWhichIndex-SortIndexFirst).setStuAnswer("J");
                        }
                    }else if( choiceResult.equals("k")){
                        if(OptionSize>=9) {
                            CNtxlPaperEntity.get(quesIndex).getFavouriteAndNote().getChildList().
                                    get(0).getSubChildList().get(0).getSubChildItem().get(clickWhichIndex-SortIndexFirst).setStuAnswer("K");
                        }
                    }
                    refreshRecordAnswer();
                }else{
                    SkEgnManager.getInstance(getActivity()).startRecord(mCoreType, "", QType.QTYPE_EMPTY, mOnRecordListener);
                    mRecoderUtils.startRecord();
                }


            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    };

    private void refreshRecordAnswer(){

        int AnsweredCount=0;
        int AnsweredCountTrue=0;
        int ChildListSize = CNtxlPaperEntity.get(quesIndex).getFavouriteAndNote().getChildList().
                get(0).getSubChildList().get(0).getSubChildItem().size();
        Log.e("==AXC===","==================小题个数=====wwww=========="+ChildListSize);
        for(int i=0;i<ChildListSize;i++){
            String StuAnswer =  CNtxlPaperEntity.get(quesIndex).getFavouriteAndNote().getChildList().
                    get(0).getSubChildList().get(0).getSubChildItem().get(i).getStuAnswer();
            if(!StuAnswer.equals("")){
                AnsweredCount = AnsweredCount+1;
            }

            String ItemAnswer =  CNtxlPaperEntity.get(quesIndex).getFavouriteAndNote().getChildList().
                    get(0).getSubChildList().get(0).getSubChildItem().get(i).getItemAnswer();
            if(ItemAnswer.equals(StuAnswer)){
                AnsweredCountTrue = AnsweredCountTrue+1;
            }
            Log.e("==AXC===","==================完成个数=====wwww=========="+AnsweredCount);
        }
        //若作答完成则显示答案，记录作答状态，否则亦然
        if (AnsweredCount == ChildListSize) {
            CNtxlPaperEntity.get(quesIndex).getFavouriteAndNote().setSubmitState(2);
            hideRecordMiView();
        }else{
            CNtxlPaperEntity.get(quesIndex).getFavouriteAndNote().setSubmitState(1);
        }
        Log.e("==mAdapter===","=======此处刷新AnswerAdapter============"+"不知道");

        list.clear();
        int totalItemCountA = CNtxlPaperEntity.get(quesIndex).getFavouriteAndNote().getChildList().size();
        for(int i=0;i<totalItemCountA;i++) {
            final RecyclerView mRvAnswer = new RecyclerView(getActivity());
            mRvAnswer.setLayoutManager(new LinearLayoutManager(getActivity()));//必须先设置LayoutManager
            CNtxlPptAnswerAdapter mAnswerAdapter = new CNtxlPptAnswerAdapter(getActivity(),
                    CNtxlPaperEntity,quesIndex,new CNtxlPptAnswerAdapter.CallBackAdapterPpt() {

                @Override
                public void callHideRecordMiView() {

                }

                @Override
                public void callClickWhichIndex(String stuAnswer) {

                }

                @Override
                public void callRefreshClickAnswer() {

                }
            });
            mRvAnswer.setAdapter(mAnswerAdapter);
            list.add(mRvAnswer);
        }
        mVpAnswer.setAdapter(new AnswerViewPagerAdapter(list));
        writeStuAnswer();

        //更新完后重新开启录音语音识别
        int SubmitState = CNtxlPaperEntity.get(quesIndex).getFavouriteAndNote().getSubmitState();
        if(SubmitState==2){
            SkEgnManager.getInstance(getActivity()).stopRecord();
            mRecoderUtils.stopRecord();
//            SaveItemQuesAnswer(quesIndex);
        }else {
            SkEgnManager.getInstance(getActivity()).startRecord(mCoreType, "", QType.QTYPE_EMPTY, mOnRecordListener);
            mRecoderUtils.startRecord();
        }
    }

    private String getResult(String result) {

        String choiceSoundResult = "";
        StringBuilder keyValue = new StringBuilder();
        try{
            JSONObject returnObj = new JSONObject(result);
            JSONObject resultJSONObject = returnObj.getJSONObject("result");
            if(resultJSONObject.has("overall")){
                keyValue.append("总    分: " + resultJSONObject.getString("overall") + "\n");
            }
            Log.e("getResultAAAAA", "====返回json========答案=========>" + resultJSONObject.getString("recognition"));
            choiceSoundResult = resultJSONObject.getString("recognition");
//            notifyDataSetChanged();

        }catch (Exception e){
            e.printStackTrace();
        }
        keyValue.append("\n");
        keyValue.append(JSONTool.stringToJSON(result));

        return choiceSoundResult;
    }
    /***************************************************************************/

    public interface CallBackFragmentPpt {
//        void pageMoveNext();
          void submitKgtQuesAnswer();
    }

    private void showRecordVolumeSize(final int volumesize){
        new Thread(new Runnable() {
            @Override
            public void run() {
                Message msg =Message.obtain();
                msg.obj = volumesize;
                msg.what = 1;
                handler.sendMessage(msg);

                try {
                    Thread.sleep(200);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

            }
        }).start();

    }

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1:
                    int VolumeSize = Integer.valueOf(msg.obj.toString());
                    if(VolumeSize>50){
                        VolumeSize=50;
                    }
                    int LevelSize = VolumeSize*200;
                    drawableMi.setLevel(LevelSize);
                    break;
            }
        }
    };

    private void writeStuAnswer(){
        Log.e("LOGTAG", "===========onReceiveValue value=============" + clickWhichIndex);
        int SubmitState = CNtxlPaperEntity.get(quesIndex).getFavouriteAndNote().getSubmitState();
        int SubChildItemSize = CNtxlPaperEntity.get(quesIndex).getFavouriteAndNote().getChildList().get(0).
                getSubChildList().get(0).getSubChildItem().size();
        for(int j=0;j<SubChildItemSize;j++) {
            String StuAnswer = CNtxlPaperEntity.get(quesIndex).getFavouriteAndNote().getChildList().
                    get(0).getSubChildList().get(0).getSubChildItem().get(j).getStuAnswer();
            String ItemAnswer = CNtxlPaperEntity.get(quesIndex).getFavouriteAndNote().getChildList().
                    get(0).getSubChildList().get(0).getSubChildItem().get(j).getItemAnswer();
            int SortIndex = CNtxlPaperEntity.get(quesIndex).getQuesFromTK().getChildList().
                    get(0).getSubChildList().get(0).getSubChildItem().get(j).getSortIndex();

            if(clickWhichIndex==SortIndex) {

                mWvQuesBody.loadUrl("javascript:setBackGroundColor('" + "blank" + clickWhichIndex +
                        "','" + "#326DE9" + "')");

                if(SubmitState==2){
                    if (ItemAnswer.equals(StuAnswer)) {
                        mWvQuesBody.loadUrl("javascript:CalledByJavaCodeWithPara('" + "blank" + SortIndex + "','" +
                                "<font color=\"#00b30d\">" + SortIndex +"." + StuAnswer + "</font>')");
                    } else {
                        mWvQuesBody.loadUrl("javascript:CalledByJavaCodeWithPara('" + "blank" + SortIndex + "','" +
                                "<font color=\"#ff0000\">" + SortIndex + "." + StuAnswer + "</font>"+
                                "<font color=\"#00b30d\">("+ItemAnswer+")"+"</font>"+"')");
                    }
                }else {
                    if (StuAnswer.equals("")) {
                        mWvQuesBody.loadUrl("javascript:CalledByJavaCodeWithPara('" + "blank" + SortIndex +
                                "','" + "<font color=\"#ffffff\">" + SortIndex + "</font>')");
                    } else {
                        mWvQuesBody.loadUrl("javascript:CalledByJavaCodeWithPara('" + "blank" + SortIndex +
                                "','" + "<font color=\"#ffffff\">" + SortIndex + "." + StuAnswer + "</font>')");
                    }
                }

            }else{

                mWvQuesBody.loadUrl("javascript:setBackGroundColor('" + "blank" + SortIndex +
                        "','" + "#00000000" + "')");

                if(SubmitState==2){
                    if (ItemAnswer.equals(StuAnswer)) {
                        mWvQuesBody.loadUrl("javascript:CalledByJavaCodeWithPara('" + "blank" + SortIndex + "','" +
                                "<font color=\"#00b30d\">" + SortIndex +"." + StuAnswer + "</font>')");
                    } else {
                        mWvQuesBody.loadUrl("javascript:CalledByJavaCodeWithPara('" + "blank" + SortIndex + "','" +
                                "<font color=\"#ff0000\">" + SortIndex + "." + StuAnswer + "</font>"+
                                "<font color=\"#00b30d\">("+ItemAnswer+")"+"</font>"+"')");
                    }
                }else {
                    mWvQuesBody.loadUrl("javascript:setBackGroundColor('" + "blank" + SortIndex +
                            "','" + "#00000000" + "')");
                    if (StuAnswer.equals("")) {
                        mWvQuesBody.loadUrl("javascript:CalledByJavaCodeWithPara('" + "blank" + SortIndex +
                                "','" + "<font color=\"#326DE9\">" + SortIndex + "</font>')");
                    } else {
                        mWvQuesBody.loadUrl("javascript:CalledByJavaCodeWithPara('" + "blank" + SortIndex +
                                "','" + "<font color=\"#326DE9\">" + SortIndex + "." + StuAnswer + "</font>')");
                    }
                }
            }

        }
    }

}
